﻿var Developer = {}

Developer._id = null;
Developer._taskDataSource = null;
Developer._developerValidator = null;

/// <summary>Initialize the developer partial view.</summary>
Developer.init = function () {
    Developer._id = $("#DeveloperId").val();

    // Initialize the validator for the developerForm form.
    Developer._developerValidator = $("#developerForm").kendoValidator({
        rules: {
            custom: function (input) {
                if (input.length > 0) {
                    var valid = true;
                    if (input.is("#LastName")) {
                        valid = input.val().length > 0 || $("#FirstName").val().length > 0;
                    } else if (input.is("#FirstName")) {
                        Developer._developerValidator.validateInput(document.getElementById("LastName"));
                    }
                    return valid;
                } else {
                    return true;
                }
            }
        },
        messages: {
            custom: function (input) {
                if (input.is("#LastName")) {
                    return "Either First Name or Last Name must be entered.";
                } else {
                    return "";
                }
            }
        }
    }).data("kendoValidator");

    // Get the tasks assigned to the developer.
    Developer._taskDataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: _rootUrl + "Home/GetDeveloperTasks/" + Developer._id
            }
        },
        schema: {
            model: {
                id: "DeveloperId",
                fields: {
                    Id: { type: "number" },
                    Type: { type: "number" },
                    EstimatedHours: { type: "number" },
                    Description: { type: "string" },
                    AssignedToId: { type: "number" },
                    SprintId: { type: "number" }
                }
            }
        }
    });

    // Display the tasks assigned to the developer in a grid.
    $("#taskGrid").kendoGrid({
        dataSource: Developer._taskDataSource,
        height: 400,
        columns: [{
            field: "SprintId",
            title: "Sprint",
            width: "100px"
        }, {
            field: "TypeOfTask",
            title: "Type",
            template: "#= TypeOfTask == 1 ? 'Scenerio' : 'Change Request' #",
            width: "200px"
        }, {
            field: "Description",
            title: "Description"
        }, {
            field: "EstimatedHours",
            title: "Estimated Hours",
            width: "200px"
        }],
        groupable: true
    });

    // Initialize the Save button.
    $("#saveButton").click(Developer.save);
}

/// <summary>Save the developer information.</summary>
Developer.save = function () {
    var messageWindow = $("#messageWindow");
    var kendoWindow = messageWindow.data("kendoWindow");

    // Has the kendoWindow been created yet?
    if (kendoWindow == undefined) {
        // Create the kendoWindow.
        kendoWindow = messageWindow.kendoWindow({
            height: "200px",
            width: "300px",
            draggable: false,
            modal: false,
            visible: false,
            resizable: false
        }).data("kendoWindow");
    } else {
        // Empty the contents of the message window.
        messageWindow.empty();
    }

    if (Developer._developerValidator.validate()) {
        $.ajax({
            type: "POST",
            dataType: "json",
            data: $("#developerForm").serialize(),
            url: _rootUrl + "Home/UpdateDeveloper"
        })
        .done(function () {
            // Display the save completed modal window.
            messageWindow.text("The Developer was successfully saved");
            kendoWindow.title("Developer Saved").center().open();
 
            // Display the updates to the developer's name in the navigation panelbar.
            Layout.Navigation.syncNavPanelBar(Layout.Navigation.expandDeveloperItem);
        });
    } else {
        // Display the save failed modal window.
        messageWindow.text("Unable to save the Developer.  There are validation errors.");
        var errors = Developer._developerValidator.errors();
        $.each(errors, function (idx, error) {
            var p = $("<p/>").text(error);
            p.appendTo(messageWindow);
        });
        kendoWindow.title("Developer Save Failed").center().open();
    }
}